package com.app.micaihu.view.main.msgcenter;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseListActivity;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.comment.MyComment;
import com.app.micaihu.bean.message.MsgList;
import com.app.micaihu.bean.message.UserMessage;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.view.main.msgcenter.adapter.MsgListAdapter;
import com.google.gson.reflect.TypeToken;
import com.app.micaihu.utils.InteractiveUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Guo on 2016/9/27.
 * <p>
 * <p>
 * 消息中心中 关于评论、@我，系统通知，（赞，高版本加入）的activity列表
 * //消息分类 1评论 2@我 3消息提醒 4粉丝
 */
public class MsgCenterListActivity extends BaseListActivity<MsgList> {

    private String msgType;//1评论 2@我 3消息提醒 4粉丝
    private String msgTitleName = "";//当前页面的title

    public static final String TITLE_NAME = "title_name";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent != null) {
            msgType = intent.getStringExtra(Config.Extra.PARAMETER1);
            msgTitleName = intent.getStringExtra(TITLE_NAME);
        }
        super.onCreate(savedInstanceState);
    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view.getId() == R.id.empty_text_go) {

            setResult(RESULT_OK);
            onBackPressed();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void changeView() {
        super.changeView();
        setTitle(msgTitleName);
        if (mLoadView != null) {
            mLoadView.setEmptyGoClickListener(this);
        }
    }

    /**
     * 去除 老版话题.
     *
     * @param list
     * @return
     */
    private List<MsgList> removeOld(List<MsgList> list) {
        if (list == null) {
            list = new ArrayList<MsgList>();
        }
        if (list.isEmpty()) {
            return list;
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == null) {
                list.remove(i);
                i--;
            } else {
                if (TextUtils.isEmpty(TypeConstant.getNewsTypeName(list.get(i).getArticleType()))) {
                    list.remove(i);
                    i--;
                }
            }
        }
        return list;
    }

    @Override
    protected void changeData(final boolean isRefresh) {
        Map<String, String> params = new HashMap<>();
        if (UserInforManager.getInstance().isLogin()) {
            params.put("uid", UserInforManager.getInstance().getUserInfor().getUid());
        }
        params.put("page", dataPage + "");
        params.put("type", msgType + "");
        HttpListener<DataBean<UserMessage>> msgListener = new HttpListener<DataBean<UserMessage>>() {
            @Override
            public void onStart() {
                super.onStart();
                setLoadView(LOADING, null);
            }

            @Override
            public void onError(VolleyError error) {
                setLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror));
            }

            @Override
            public void onSuccess(DataBean<UserMessage> response) {
                if (!response.noError()) {
                    setLoadView(DATAERROR, getString(R.string.dataerror));
                } else {
                    UserMessage userMessage = response.getData();
                    if (userMessage == null) return;

                    List<MsgList> msgList = userMessage.getMsgList();
                    if (msgList != null && msgList.size() > 0) {
                        //去重 老话题
                        removeOld(msgList);

                        if (dataList == null) {
                            dataList = new ArrayList<MsgList>();
                        }
                        for (int i = 0; i < msgList.size(); i++) {
                            if (InteractiveUtils.getInstance().theDefriendIsContains(msgList.get(i).getUid())) {
                                msgList.remove(msgList.get(i));
                                i--;
                            }
                        }
                        if (isRefresh) {
                            //清空数据
                            dataList.clear();
                        }
                        dataList.addAll(msgList);
                        if (mAdapter == null) {
                            mAdapter = new MsgListAdapter(dataList, MsgCenterListActivity.this, msgType);
                            mListView.setAdapter(mAdapter);
                        } else {
                            mAdapter.notifyDataSetChanged();
                        }
                        setLoadView(LOADSUCES, null);
                    } else {
                        showEmptyView();
                    }
                }
            }
        };
        addPostCacheRequest(URIContant.MESSAGE_LIST, new TypeToken<DataBean<UserMessage>>() {
        }.getType(), params, msgListener);
    }

    /**
     * 无消息时候的显示UI方法
     */
    private void showEmptyView() {
        if (dataList == null) {
            if (TextUtils.equals(msgType, TypeConstant.MSG_COMMENT)) {
                setLoadView(DATAEMPTY, TypeConstant.EMPTYVIEW_CLOCK, getString(R.string.msg_empty_comment), null, getString(R.string.msg_empty_go));
            } else if (TextUtils.equals(msgType, TypeConstant.MSG_AT)) {
                setLoadView(DATAEMPTY, TypeConstant.EMPTYVIEW_DEFAULT, getString(R.string.msg_empty_at), null, getString(R.string.msg_empty_go));
            } else if (TextUtils.equals(msgType, TypeConstant.MSG_NOTICE)) {
                setLoadView(DATAEMPTY, TypeConstant.EMPTYVIEW_CLOCK, getString(R.string.msg_empty_notice), null, getString(R.string.msg_empty_go));
            } else if (TextUtils.equals(msgType, TypeConstant.MSG_ZAN)) {
                setLoadView(DATAEMPTY, TypeConstant.EMPTYVIEW_DEFAULT, getString(R.string.msg_empty_zan), null, getString(R.string.msg_empty_go));
            }
        } else {
            if (TextUtils.equals(msgType, TypeConstant.MSG_COMMENT)) {
                setLoadView(LOADSUCES, getString(R.string.msg_nomore_comment));
            } else if (TextUtils.equals(msgType, TypeConstant.MSG_AT)) {
                setLoadView(LOADSUCES, getString(R.string.msg_nomore_at));
            } else if (TextUtils.equals(msgType, TypeConstant.MSG_NOTICE)) {
                setLoadView(LOADSUCES, getString(R.string.msg_nomore_notice));
            } else if (TextUtils.equals(msgType, TypeConstant.MSG_ZAN)) {
                setLoadView(LOADSUCES, getString(R.string.msg_nomore_zan));
            }
        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
